package com.yhos.platform.oopston.service.biz;

import com.yhos.platform.oopston.base.config.OopstonConfig;
import com.yhos.platform.oopston.base.constants.CommonConstants;
import com.yhos.platform.oopston.base.enums.ShippingMethodEnum;
import com.yhos.platform.oopston.core.exception.BusinessException;
import com.yhos.platform.oopston.core.mybatis.CommonMapper;
import com.yhos.platform.oopston.core.mybatis.SnowFlake;
import com.yhos.platform.oopston.core.utils.ListUtils;
import com.yhos.platform.oopston.core.utils.MapperUtils;
import com.yhos.platform.oopston.core.utils.WebUtil;
import com.yhos.platform.oopston.mapper.entity.*;
import com.yhos.platform.oopston.mapper.mapper.*;
import com.yhos.platform.oopston.mapper.model.shunfeng.ShunfengOrderCreateRequestApiModel;
import com.yhos.platform.oopston.mapper.model.shunfeng.ShunfengOrderCreateResponseApiModel;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2020/4/19.
 */
@Service
public class ShunfengOrderLabelBiz {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OopstonConfig oopstonConfig;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private ShunfengOrderMapper shunfengOrderMapper;

    @Autowired
    private ShunfengOrderPartyMapper shunfengOrderPartyMapper;

    @Autowired
    private ShunfengOrderPartyAddressMapper shunfengOrderPartyAddressMapper;

    @Autowired
    private ShunfengOrderParcelMapper shunfengOrderParcelMapper;


    @Autowired
    private ShunfengOrderParcelProductMapper shunfengOrderParcelProductMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ShunfengOrder createOrderInfo(ShunfengOrderCreateRequestApiModel requestApiModel){
        ShunfengOrder order = MapperUtils.mapper(requestApiModel, ShunfengOrder.class);
        order.setId(SnowFlake.getInstance().nextId());
        order.setOrderSn(requestApiModel.getOrder_sn());
        commonMapper.setBaseEntityAdd(order, CommonConstants.LONG_ZERO);

        //保存发送方
        List<ShunfengOrderParty> senders = MapperUtils.mapper(requestApiModel.getSender(), ShunfengOrderParty.class);
      //  List<ShunfengOrderPartyAddress> addresses = MapperUtils.mapper(requestApiModel.sender., ShunfengOrderParty.class);
         if (ListUtils.isNotEmpty(senders)) {
            for (ShunfengOrderParty sender : senders) {
                sender.setId(SnowFlake.getInstance().nextId());
                sender.setOrderId(order.getId());
                commonMapper.setBaseEntityAdd(sender, CommonConstants.LONG_ZERO);
                shunfengOrderPartyMapper.insert(sender);
            }
        }
        //保存接收方
        List<ShunfengOrderParty> receivers = MapperUtils.mapper(requestApiModel.getReceiver(), ShunfengOrderParty.class);
        if (ListUtils.isNotEmpty(receivers)) {
            for (ShunfengOrderParty receiver : receivers) {
                receiver.setId(SnowFlake.getInstance().nextId());
                receiver.setOrderId(order.getId());
                commonMapper.setBaseEntityAdd(receiver, CommonConstants.LONG_ZERO);
                shunfengOrderPartyMapper.insert(receiver);
            }
        }

        //包裹信息
        List<ShunfengOrderParcel> parcels = MapperUtils.mapper(requestApiModel.getParcel(), ShunfengOrderParcel.class);
        if (ListUtils.isNotEmpty(parcels)) {
            for (ShunfengOrderParcel parcel : parcels) {
                parcel.setId(SnowFlake.getInstance().nextId());
                parcel.setOrderId(order.getId());
                commonMapper.setBaseEntityAdd(parcel, CommonConstants.LONG_ZERO);
                shunfengOrderParcelMapper.insert(parcel);
            }
        }

        shunfengOrderMapper.insert(order);
        return order;
    }

    /**
     * 更新回填信息
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateResponse(Long orderId, ShunfengOrderCreateResponseApiModel responseApiModel) {
        ShunfengOrder order = new ShunfengOrder();
        order.setId(orderId);
        order.setOrderSn(responseApiModel.getOrder_sn());

        commonMapper.setBaseEntityModify(order,CommonConstants.LONG_ZERO);
        shunfengOrderMapper.updateById(order);

/*        List<OopstonOrderLabelItem> labels = MapperUtils.mapper(responseApiModel.getLabels(), OopstonOrderLabelItem.class);
        if(ListUtils.isNotEmpty(labels)){
            for(OopstonOrderLabelItem label : labels){
                label.setId(SnowFlake.getInstance().nextId());
                label.setOrderId(orderId);
                commonMapper.setBaseEntityAdd(label,CommonConstants.LONG_ONE);
                oopstonOrderLabelItemMapper.insert(label);
            }
        }*/
    }
/*
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public OopstonOrder createZhongTongOrderInfo(ZhongTongOrderCreateRequestApiModel requestApiModel){
        OopstonOrder order = MapperUtils.mapper(requestApiModel, OopstonOrder.class);
        order.setId(SnowFlake.getInstance().nextId());
        order.setOrderRefNo(requestApiModel.getReferenceNo());
        order.setTotalQuantity(requestApiModel.getQuantity());
        order.setTotalWeight(requestApiModel.getWeight());
        if (requestApiModel.getQuantity() > 1) {
            order.setBaseChannelInfoId(oopstonConfig.getBigPackChannelId());
        } else {
            order.setBaseChannelInfoId(oopstonConfig.getSmallPackChannelId());
        }
        String shippingMethod = ShippingMethodEnum.getValueByKey(requestApiModel.getShippingMethod());
        if(StringUtils.isBlank(shippingMethod)){
            throw new BusinessException("运输方式输入有误");
        }
        commonMapper.setBaseEntityAdd(order, CommonConstants.LONG_ZERO);
        shunfengOrderMapper.insert(order);

        List<OopstonOrderSkuItem> orderProducts = MapperUtils.mapper(requestApiModel.getOrderProducts(), OopstonOrderSkuItem.class);
        if (ListUtils.isNotEmpty(orderProducts)) {
            for (OopstonOrderSkuItem orderProduct : orderProducts) {
                orderProduct.setId(SnowFlake.getInstance().nextId());
                orderProduct.setOrderId(order.getId());
                commonMapper.setBaseEntityAdd(orderProduct, CommonConstants.LONG_ZERO);
                oopstonOrderSkuItemMapper.insert(orderProduct);
            }
        }

        List<OopstonOrderExtraService> orderExtraServices = MapperUtils.mapper(requestApiModel.getExtraServices(), OopstonOrderExtraService.class);
        if (ListUtils.isNotEmpty(orderExtraServices)) {
            for (OopstonOrderExtraService orderExtraService : orderExtraServices) {
                orderExtraService.setId(SnowFlake.getInstance().nextId());
                orderExtraService.setOrderId(order.getId());
                commonMapper.setBaseEntityAdd(orderExtraService, CommonConstants.LONG_ZERO);
                oopstonOrderExtraServiceMapper.insert(orderExtraService);
            }
        }
        return order;
    }*/

    /**
     * 更新回填信息
     */
/*    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateZhongTongResponse(Long orderId, ZhongTongOrderCreateRemoteResponse responseApiModel) {
        OopstonOrder order = new OopstonOrder();
        order.setId(orderId);
        order.setResponseOrderId(responseApiModel.getData().getOrder_id());
        order.setResponseTrackNumber(responseApiModel.getData().getShipping_method_no());
        commonMapper.setBaseEntityModify(order,CommonConstants.LONG_ZERO);
        shunfengOrderMapper.updateById(order);
    }*/

    /**
     * 更新回填信息
     */
/*    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateZhongTongGetTagResponse(Long orderId, ZhongTongGetNewLabelRemoteResponse responseApiModel) {
        OopstonOrder order = new OopstonOrder();
        order.setId(orderId);
        if(ListUtils.isNotEmpty(responseApiModel.getData())){
            order.setResponsePrintUrl(responseApiModel.getData().get(0).getLableFile());
            commonMapper.setBaseEntityModify(order,CommonConstants.LONG_ZERO);
            shunfengOrderMapper.updateById(order);
        }
    }*/
}
